<?php

namespace App\Common\Comp\Examine;
use App\Common\RedisCache\LiveRoomHashCache;
use App\Common\RedisCache\HotlineCache;
use Illuminate\Support\Facades\Log;

class ContentCheck
{

    /**
     * 内容检查
     * @param  [type] $data 参数数组
     * @return [type]       [description]
     */
    public static function exec($data)
    {
        $type           = $data['type'] ?? '';//检测类型1-文本，2-图片，3-视频，4-直播视频流
        $sceneId        = $data['sceneId'] ?? '';//场景
        $dataId         = $data['dataId'] ?? '';//数据标识ID
        $content        = $data['content'] ?? '';//数据内容
        $userId         = $data['userId'] ?? '';//用户ID
        $extraData      = $data['extraData'] ?? [];//扩展参数
        Log::info(" ContentCheck1_____".$type);

        try {
            if(empty($type) || empty($sceneId)){
                throw new \Exception('缺少参数');
            }
            switch ($type) {
                case 1://文本检测
                    $result = self::textCheck($sceneId,$dataId,$userId,$content,$extraData);
                    break;
                case 2://图片检测
                    $result = self::imgCheck($sceneId,$dataId,$userId,$content,$extraData);
                    break;
                case 3://视频检测
                    $result = self::videoCheck($sceneId,$dataId,$userId,$content);
                    break;
                case 4://直播检测
                    $result = self::liveCheck($sceneId,$dataId,$userId,$content);
                    break;
                case 5://语音检测
                    $result = self::audioCheck($sceneId,$dataId,$userId,$content);
                    break;
                case 6://反作弊
                    $result =  ['code'=>200 ,'msg'=>'','data'=>[]];
                    //$result = self::antiCheating($sceneId,$dataId,$userId,$content);
                    break;
                case 7://行为式验证码
                    $result = self::verificationCode($sceneId,$dataId,$userId,$content);
                    break;
                default:
                    $result = [];
            }

            return $result;
        } catch (\Exception $e) {
            Log::info(" ContentCheck_____".json_encode($e->getMessage()));
            Log::error('内容监测错误:' . $e->getMessage());
            return false;
        }
    }

    /**
     * 文本检测
     * @param  [type] $sceneId      场景
     * @param  [type] $dataId       数据标识ID
     * @param  [type] $userId       用户ID
     * @param  [type] $content      数据内容
     * @return [type]               [description]
     */
    private static function textCheck($sceneId,$dataId,$userId,$content,$extraData)
    {
        $result = [];
        if(in_array($sceneId,[1,2,3,4,5,6,7,8,23,25,42,43,44,61,63,64])){
            if(is_array($content)){
                //批量检测接口
                $result = (new YiDun())::textBatchCheck($dataId,$userId,$content,$sceneId);
            }else{
                $result = (new YiDun())::textCheck($dataId,$userId,$content,$sceneId,$extraData);
            }

        } elseif (in_array($sceneId,[22,24,26,27,28,29,30,31,59])){
            if(is_array($content)){
                $result = (new ShuMei())::textBatchCheck($dataId,$userId,$content,$sceneId);
            }else{
                $result = (new ShuMei())::textCheck($dataId,$userId,$content,$sceneId);
            }

        }
        return $result;
    }

    /**
     * 图片检测
     * @param  [type] $sceneId      场景
     * @param  [type] $dataId       数据标识ID
     * @param  [type] $userId       用户ID
     * @param  [type] $content      数据内容
     * @return [type]               [description]
     */
    private static function imgCheck($sceneId,$dataId,$userId,$content,$extraData)
    {
        $result = [];
        if(in_array($sceneId,[9,12,15,16,40,41,65,66])){
            $result = (new YiDun())::imgCheck($dataId,$userId,$content,$sceneId,$extraData);
        } elseif (in_array($sceneId,[32,33,34,35,36,58,60])){
            $result = (new ShuMei())::imgCheck($dataId,$userId,$content,$sceneId);
        }
        return $result;
    }

    /**
     * 视频检测
     * @param  [type] $sceneId      场景
     * @param  [type] $dataId       数据标识ID
     * @param  [type] $userId       用户ID
     * @param  [type] $content      数据内容
     * @return [type]               [description]
     */
    private static function videoCheck($sceneId,$dataId,$userId,$content)
    {
        $result = [];
        if(in_array($sceneId,[13,14,17,18,19,35,36,57])){
            $result = (new YiDun())::videoCheck($dataId,$userId,$content,$sceneId);
        }elseif (in_array($sceneId,[38,62])){
            $result = (new YiDun())::videoSync($dataId,$userId,$content,$sceneId);
        }
        return $result;
    }

    /**
     * 直播检测
     * @param  [type] $sceneId      场景
     * @param  [type] $dataId       数据标识ID/liveLogId
     * @param  [type] $userId       用户ID
     * @param  [type] $content      数据内容
     * @return [type]               [description]
     */
    private static function liveCheck($sceneId,$dataId,$userId,$content)
    {
        $result = [];
        if(in_array($sceneId,[10])){
            $result = (new YiDun())::liveCheck($dataId,$userId,$content,$sceneId);
            //加入缓存
            if($result['code'] == 200){
                $taskId=$result['data']['taskId'];
                $dataId=$result['data']['dataId'];
                $LiveRoomHashCache = new LiveRoomHashCache();
                $LiveRoomHashCache->setKeyParameter(['prefix'=>$dataId]);
                $LiveRoomHashCache->setFieldParameter(['unique' => 'taskId'])->set($taskId);

            }
        }elseif (in_array($sceneId,[37])){
            $result = self::feedback($sceneId,$dataId,$userId,$content);
        }elseif (in_array($sceneId,[11])){
            $result = (new YiDun())::liveWallSolution($dataId,$userId,$content,$sceneId);
            if(isset($result['data'])){
                //存入redis
                $key=$result['data']['key'];
                $HotlineCache = new HotlineCache();
                $HotlineCache->setKeyParameter(['prefix' => $key]);
                $HotlineCache->set($result['data']['taskId']);
            }

        }elseif (in_array($sceneId,[39])){
            $result = self::feedback($sceneId,$dataId,$userId,$content);
        }elseif (in_array($sceneId,[45])){
            $result = self::liveaudioCheck($sceneId,$dataId,$userId,$content);
        }elseif (in_array($sceneId,[46])){
            $result = self::liveaudioFeedback($sceneId,$dataId,$userId,$content);
        }
        return $result;
    }

    /**
     * 反作弊拦截
     * @param  [type] $sceneId [description]
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @return [type]          [description]
     */
    private static function antiCheating($sceneId,$dataId,$userId,$content)
    {
        $result = [];
        $result = (new YiDun())::antiCheating($dataId,$userId,$content,$sceneId);
        Log::info("ContentCheck1_____".json_encode($result,JSON_UNESCAPED_UNICODE));
        return $result;
    }


    /**
     * 停止直播检测
     * @param  [type] $sceneId [description]
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @return [type]          [description]
     */
    private static function feedback($sceneId,$dataId,$userId,$content)
    {
        $result = (new YiDun())::feedback($dataId,$userId,$content,$sceneId);
        return $result;
    }

    /**
     * 停止语音直播检测
     * @param  [type] $sceneId [description]
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @return [type]          [description]
     */
    private static function liveaudioFeedback($sceneId,$dataId,$userId,$content)
    {
        $result = (new YiDun())::liveaudioFeedback($dataId,$userId,$content,$sceneId);
        return $result;
    }


    /**
     * 语音检测
     * @param  [type] $sceneId      场景
     * @param  [type] $dataId       数据标识ID
     * @param  [type] $userId       用户ID
     * @param  [type] $content      数据内容
     * @return [type]               [description]
     */
    private static function audioCheck($sceneId,$dataId,$userId,$content)
    {
        $result = [];
        $result = (new YiDun())::audioCheck($dataId,$userId,$content,$sceneId);
        return $result;
    }


    /**
     * 语音直播检测
     * @param  [type] $sceneId      场景
     * @param  [type] $dataId       数据标识ID/liveLogId
     * @param  [type] $userId       用户ID
     * @param  [type] $content      数据内容
     * @return [type]               [description]
     */
    private static function liveaudioCheck($sceneId,$dataId,$userId,$content)
    {

        $result = (new YiDun())::liveaudioCheck($dataId,$userId,$content,$sceneId);
        //加入缓存
        if($result['code'] == 200){
            $taskId=$result['data']['taskId'];
            $dataId=$result['data']['dataId'];
            $LiveRoomHashCache = new LiveRoomHashCache();
            $LiveRoomHashCache->setKeyParameter(['prefix'=>$dataId]);
            $LiveRoomHashCache->setFieldParameter(['unique' => 'taskId'])->set($taskId);

        }
        return $result;
    }



    /**
     * 行为式验证码
     * @param  [type] $sceneId [description]
     * @param  [type] $dataId  [description]
     * @param  [type] $userId  [description]
     * @param  [type] $content [description]
     * @return [type]          [description]
     */
    private static function verificationCode($sceneId,$dataId,$userId,$content)
    {
        $result = [];
        $result = (new YiDun())::verificationCode($dataId,$userId,$content,$sceneId);
        return $result;
    }


}
